<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/18 0018
 * Time: 14:51
 */

namespace app\admin\controller;


use think\Request;

class Partners extends Base
{
    protected $_Partners;

    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_Partners = new \app\model\model\Partners();

    }

    /*=======================合作伙伴===========================*/
    public function lists(){
        return $this->fetch();
    }
    public function getList(){
        $where = [];
        $name = input('name');
        if($name){
            $where['name'] = ['like',"%$name%"];
        }

        $data = $this->_Partners->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            $v['img_url']='<img style="height: 50px;width: 80px;" src="'.$v['img_url'].'">';
        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 添加
     * */
    public function add(){
        if(!Request::instance()->post()){
            return $this->fetch();
        }else{

            $data = [
                'name'=>input('name'),
                'img_url'=>1,
                'guojia'=>input('guojia'),
                'create_time'=>time(),
                'alt'=>1
            ];
            $re = $this->_Partners->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function up(){
        if(!Request::instance()->post()){
            $data = $this->_Partners->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data = [
                'name'=>input('name'),
                'guojia'=>input('guojia'),

            ];


            $re = $this->_Partners->up(['id'=>input('id')],$data);
            if($re){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOne(){
        $data = $this->_Partners->getOne(input('id'));
        $data['img_url']='<img style="height: 50px;width: 80px;" src="'.$data['img_url'].'">';
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 完全删除
     * */
    public function del(){
        $re = $this->_Partners->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量完全删除
     * */
    public function delAll(){
        $re = $this->_Partners->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 排序
     * */
    public function sort(){
        $data = ['sort'=>input('sort')];
        $re = $this->_Partners->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'成功！');
        }else{
            return json_echo_msg(400,'失败！');
        }
    }
}